package com.example.websocketdemo.controller;

import com.example.websocketdemo.mongoConfig.MongoTestDao;
import com.example.websocketdemo.pojo.ServerMongodb;
import com.example.websocketdemo.util.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Clock;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @ClassName mongodbController
 * @Author xiaojiang
 * @Date 2021/9/28 下午 12:10
 */
@RestController
public class mongodbController {

    @Autowired
    MongoTestDao mongoTestDao;

    /**
     *  TODO  使用默认集合保存
     * @Param : request
     * @Param : response
     * @return HttpResult
     * @Author 江
     * @Date 2021/9/28 下午 12:16
     */
    @GetMapping("/saveMongodb")
    public HttpResult saveMongodb(HttpServletRequest request, HttpServletResponse response) {
        ServerMongodb serverMongodb = new ServerMongodb();
        serverMongodb.setD1(1L);
        serverMongodb.setD2(2L);
        serverMongodb.setD3(3L);
        serverMongodb.setD4(4L);
        mongoTestDao.saveTest(serverMongodb);

        return HttpResult.newSuccess();
    }

    /**
     *  TODO  使用指定集合进行保存数据
     * @Param : request
     * @Param : response
     * @return HttpResult
     * @Author 江
     * @Date 2021/9/28 下午 12:50
     */
    @GetMapping("/insterMongodb")
    public HttpResult insterMongodb(HttpServletRequest request, HttpServletResponse response) {
        ServerMongodb serverMongodb = new ServerMongodb();
        serverMongodb.setD1(1L);
        serverMongodb.setD2(2L);
        serverMongodb.setD3(3L);
        serverMongodb.setD4(4L);
        mongoTestDao.insertManyTest("1921683127",serverMongodb);

        return HttpResult.newSuccess();
    }

    /**
     *  TODO  根据参数取出指定集合的数据
     * @Param : request
     * @Param : response
     * @return HttpResult
     * @Author 江
     * @Date 2021/9/28 下午 12:51
     */
    @GetMapping("/findTestByName")
    public HttpResult findTestByName(HttpServletRequest request, HttpServletResponse response) {

        ServerMongodb testByName = mongoTestDao.findTest(1, "1921683127");

        return HttpResult.newSuccess().add("data",testByName);
    }

    /**
     *  TODO  取出指定集合的所有数据
     * @Param : request
     * @Param : response
     * @return HttpResult
     * @Author 江
     * @Date 2021/9/28 下午 12:51
     */
    @GetMapping("/findTestAll")
    public HttpResult findTestAll(HttpServletRequest request, HttpServletResponse response) {

        List<ServerMongodb> testAll = mongoTestDao.findTestAll("1921683127");

        return HttpResult.newSuccess().add("data",testAll);
    }

    @GetMapping("/updateTest")
    public HttpResult updateTest(HttpServletRequest request, HttpServletResponse response) {

        ServerMongodb serverMongodb = new ServerMongodb();
        serverMongodb.setD1(1L);
        serverMongodb.setD2(23L);
        serverMongodb.setD3(34L);
        serverMongodb.setD4(45L);
        mongoTestDao.updateTest(serverMongodb,"1921683127");

        return HttpResult.newSuccess();
    }

    @GetMapping("/updateTestAll")
    public HttpResult updateTestAll(HttpServletRequest request, HttpServletResponse response) {

        ServerMongodb serverMongodb = new ServerMongodb();
        serverMongodb.setD1(1L);
        serverMongodb.setD2(22L);
        serverMongodb.setD3(33L);
        serverMongodb.setD4(44L);
        mongoTestDao.updateTestAll(serverMongodb,"1921683127");

        return HttpResult.newSuccess();
    }

    @GetMapping("/deleteTestById")
    public HttpResult deleteTestById(HttpServletRequest request, HttpServletResponse response) {

        mongoTestDao.deleteTestById(1,"1921683127");

        return HttpResult.newSuccess();
    }

    @GetMapping("/deleteTestAll")
    public HttpResult deleteTestAll(HttpServletRequest request, HttpServletResponse response) {

        mongoTestDao.deleteTestAll("1921683127");

        return HttpResult.newSuccess();
    }


    private ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(50);

    public void sendRelease() {
        scheduledThreadPool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("-=-=-=-=-=");
            }
        }, 500, TimeUnit.SECONDS);
    }

    /**
     *  TODO  jdk8 新获取秒表时间的方法
     * @Author 江
     * @Date 2021/11/12 下午 14:39
     */
    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        System.out.println(l);
        System.out.println();
        for (int i = 0; i < 1000; i++) {
            System.out.println("Clock : " + Clock.systemUTC().millis());

        }
        long ll = System.currentTimeMillis();
        System.out.println(ll);
        System.out.println(ll - l);   //  32
        /*Clock clock = Clock.systemUTC();
        Integer integer = Integer.valueOf(clock.toString());
        long millis = clock.millis();
        System.out.println("Clock : " + clock.millis());
        System.out.println("Clock : " + clock.millis());

        // Returns time based on system clock zone
        Clock defaultClock = Clock.systemDefaultZone();
        System.out.println("Clock : " + defaultClock.millis());


        long l = System.currentTimeMillis();
        System.out.println(l);*/

        /*long l = System.currentTimeMillis();
        System.out.println(l);
        System.out.println();
        for (int i = 0; i < 1000; i++) {
            System.out.println("Clock : " + System.currentTimeMillis());

        }
        long ll = System.currentTimeMillis();
        System.out.println(ll);
        System.out.println(ll - l);*/

    }
}
